package com.example.zjhuojback.mapper;

import com.example.zjhuojback.entity.Forum;
import com.example.zjhuojback.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ForumMapper {

    // 使用分页查询
    @Select("SELECT * FROM `forum` ORDER BY id DESC LIMIT #{offset}, #{limit}")

    List<Forum> selectAll(@Param("offset") int offset, @Param("limit") int limit);

    // 查询总记录数
    @Select("SELECT COUNT(*) FROM `forum`")
    long countAll();

    // 创建帖子
    @Insert("insert into `forum` (title, content, userName, userId) " +
            "values (#{title}, #{content}, #{userName}, #{userId})")
    void insert(Forum forum);

    // 更新帖子
    @Update("update `forum` set title = #{title}, updateTime = #{updateTime}, content = #{content} where id = #{id}")
    void update(Forum forum);

    // 添加评论
    @Insert("insert into `postcomment` (forumId, content, userName, userId) " +
            "values (#{title}, #{content}, #{userName}, #{userId})")
    void addcomment(Forum forum);

    // 根据帖子id搜索帖子内容
    @Select("select * from `forum` where id = #{id}")
    Forum selectById(@Param("id") Integer id);

    // 根据帖子id搜索帖子评论
    @Select("select * from `postcomment` where forumId = #{id}")
    List<Forum> getCommentsByPostId(@Param("id") Integer id);

    // 根据帖子id删除帖子内容
    @Delete("delete from `forum` where id = #{id}")
    void deleteForum(Integer id);

    // 根据帖子id删除帖子评论
    @Delete("delete from `postcomment` where forumId = #{id}")
    void deleteComment(Integer id);

    // 根据评论id删除帖子评论
    @Delete("delete from `postcomment` where id = #{id}")
    void deleteCommentByCommentId(Integer id);

    // 根据帖子id点赞帖子
    @Update("UPDATE `forum` SET thumbNum = thumbNum + 1 WHERE id = #{id}")
    void incrementThumbNumById(Integer id);

    // 根据帖子id收藏帖子
    @Update("UPDATE `forum` SET favourNum = favourNum + 1 WHERE id = #{id}")
    void incrementFavourNumById(Integer id);
}
